package com.yjzx.interfaces;

import com.yjzx.util.CollectionUtil;
import com.yjzx.util.StringUtils;

import java.util.List;

/**
 * @author yjzx
 * @date 2024/4/20
 * @description
 */
public enum ModifierEnum {
    /**
     * 关键字
     */
    public_("public", 1),
    private_("private", 2),
    protected_("protected", 4),
    static_("static", 8),
    final_("final", 16),
    synchronized_("synchronized", 32),
    volatile_("volatile", 64),
    varargs_("varargs", 128),
    transient_("transient", 128),
    native_("native", 256),
    interface_("interface", 512),
    abstract_("abstract", 1024),
    strict("strict", 2048),
    annotation("annotation", 8192),
    enum_("enum", 16384);

    private final String value;
    private final int index;

    ModifierEnum(String value, int index) {
        this.value = value;
        this.index = index;
    }

    public String getValue() {
        return value;
    }

    public int getIndex() {
        return index;
    }

    public static ModifierEnum toModifier(String value) {
        if (StringUtils.isNotBlank(value)) {
            value = value.toLowerCase();
            for (ModifierEnum modifier : ModifierEnum.values()) {
                if (modifier.value.equals(value)) {
                    return modifier;
                }
            }
        }
        throw new RuntimeException("不存在当前类型修饰符");
    }

    public static ModifierEnum toModifier(int index) {
        if (index != 0) {
            for (ModifierEnum modifier : ModifierEnum.values()) {
                if (index == modifier.index) {
                    return modifier;
                }
            }
        }
        throw new RuntimeException("不存在当前类型修饰符");
    }

    public static int modifierToInt(List<String> modifiers) {
        int i = 0;
        if (CollectionUtil.isNotEmpty(modifiers)){
            for (String modifier : modifiers) {
                i = i | toModifier(modifier).index;
            }
        }

        return i;
    }
}
